
package com.midian.action.mc.banner;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.midian.action.mc.common.AjaxAction;
import com.midian.common.annotation.FileModelDetails;
import com.midian.common.annotation.FunctionAnnotation;
import com.midian.common.annotation.FunctionAnnotationOverride;
import com.midian.common.annotation.InfoFlexIds;
import com.midian.common.exception.BizException;
import com.midian.model.activity.Activity;
import com.midian.model.activity.ActivityType;
import com.midian.model.banner.Banner;
import com.midian.model.common.FileModel;
import com.midian.service.activity.IActivityService;
import com.midian.service.activity.IActivityTypeService;
import com.midian.service.banner.IBannerService;
import com.midian.service.pic.IPicService;

/**
 * banner广告Action
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("bannerAction")
@FunctionAnnotationOverride(isAction = true, auth = "banner管理")
public class BannerAction extends AjaxAction<Banner> {
	/**banner广告实体*/
	private Banner entity;
	/**banner广告实体列表*/
	private List<Banner> rows;
	/**banner广告业务*/
	private IBannerService bannerService;
	
	private String bl;
	/**活动列表*/
	private List<Activity> activityList;
	/**活动业务*/
	private IActivityService activityService;
	/**活动广告图片*/
	private FileModel bannerPic;
	/**图片处理业务*/
	private IPicService picService;
	

	@Resource(name = "bannerService")
	public void setBannerService(IBannerService bannerService) {
		this.service = bannerService;
		this.bannerService = bannerService;
	}
	
	@Resource(name="activityService")
	public void setActivityService(IActivityService activityService) {
		this.activityService = activityService;
	}
	
	@Resource(name="picService")
	public void setPicService(IPicService picService) {
		this.picService = picService;
	}
	
	@FileModelDetails(fieldName = "bannerPic.id",isDelOldFile = false)
	public FileModel getBannerPic() {
		return bannerPic;
	}

	public void setBannerPic(FileModel bannerPic) {
		this.bannerPic = bannerPic;
	}

	public Banner getEntity() {
		return entity;
	}
	
	public void setEntity(Banner entity) {
		this.entity = entity;
	}
	
	
	public String getBl() {
		return bl;
	}
	public void setBl(String bl) {
		this.bl = bl;
	}
	
	public List<Activity> getActivityList() {
		return activityList;
	}
	public void setActivityList(List<Activity> activityList) {
		this.activityList = activityList;
	}
	
	@Override
	protected Banner dytEntity() {
		
		if(this.entity == null) {
			
			this.entity = new Banner();
		}
		return getEntity();
	}
	@Override
	protected void dyEntity(Banner t) {
		setEntity(t);
	}
	@Override
	public void setRows(List<Banner> rows) {
		this.rows = rows;
	}
	@Override
	public List<Banner> getRows() {
		return rows;
	}
	
	@Override
	public String ajax() {
		try {
//			activityTypeList=this.activityTypeService.getActivityTypes();
			activityList =this.activityService.getActivitys();
		} catch (BizException e) {
			e.printStackTrace();
		}
		return super.ajax();
	}
	
	
	@Override
	public String showInfo() {
		try {
//				activityTypeList = this.activityTypeService.getActivityTypes();
				activityList = this.activityService.getActivitys();
				if(this.id != null && !"".equals(this.id)) {
				Banner banner = service.get(this.id);
				this.bl =  StringUtils.join(banner, ",");
				}
		} catch (BizException e) {
				e.printStackTrace();
		}
		return super.showInfo();
	}
	
	@Override
	public String saveInfo() {
		try{
			Banner b = dytEntity();
			String id = dytEntity().getId();
			if(StringUtils.isBlank(id)){
				service.add(dytEntity());
			}else{
				if (b.getActivity()!=null) {
					super.setId(id); //设置Action的id属性，flexigrid操作数据使用
					service.update(dytEntity());
				}else {
					Activity activity = new Activity();
					activity.setId(null);
					dytEntity().setActivity(activity);
					super.setId(id);
					service.update(getEntity());
				}
			}
		}catch (BizException e) {
			getError(e);
		}catch (Exception e) {
			BizException be = new BizException("添加失败", e);
			getError(be);
		}
		return SAVEINFO;
	}
}
